<template>
  <div class="com-container">
    <div class="com-chart" ref="seller_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
export default {
  data() {
    return {
      chartInstance: null,
      chartData: [],
      currentPage: 1,
      totalPage: 0,
      pageSize: 5,
      timer: null,
    };
  },

  created() {
    window.addEventListener('resize', this.screenAdapter);
    // 注册回调函数
    this.$socket.registerCallBack('sellerData', this.getData);
  },

  mounted() {
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: 'getData',
      socketType: 'sellerData',
      chartName: 'seller',
      value: '',
    });
    this.screenAdapter();
  },

  beforeDestroy() {
    clearInterval(this.timer);
    window.removeEventListener('resize', this.screenAdapter);

    this.$socket.unRegisterCallBack('sellerData');
  },

  methods: {
    // 初始化echarts实例
    initChart() {
      this.chartInstance = this.$echarts.init(
        this.$refs['seller_ref'],
        this.theme
      );

      const initOption = {
        title: {
          text: '‖ 商家销售量统计',
          left: 20,
          top: 20,
        },
        // 直角坐标轴
        grid: {
          top: '20%',
          left: '3%',
          right: '6%',
          bottom: '3%',
          containLabel: true, // 距离是包含坐标轴上的文字
        },
        xAxis: {
          type: 'value',
        },
        yAxis: {
          type: 'category',
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'line',
            z: 0, // 提示的层级
            lineStyle: {
              color: '#2d3443',
            },
          },
        },
        series: [
          {
            type: 'bar',
            label: {
              show: true,
              position: 'right',
              textStyle: {
                color: 'white',
              },
            },
            itemStyle: {
              // 渐变 1. 渐变方向 2. 不同百分比之下颜色的值
              color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                {
                  offset: 0,
                  color: '#5052ee',
                },
                {
                  offset: 1,
                  color: '#ab6ee5',
                },
              ]),
            },
          },
        ],
      };
      this.chartInstance.setOption(initOption);

      this.chartInstance.on('mouseout', () => {
        this.startInterval();
      });
      this.chartInstance.on('mouseover', () => {
        clearInterval(this.timer);
      });
    },

    // async getData() {
    //   const { data } = await this.$http.get("seller");
    //   this.chartData = data;
    //   this.totalPage =
    //     this.chartData.length % 5 === 0
    //       ? this.chartData.length / this.pageSize
    //       : his.chartData.length / this.pageSize + 1;
    //   this.updateChart();
    //   this.startInterval();
    // },
    getData(data) {
      this.chartData = data;
      this.totalPage =
        this.chartData.length % 5 === 0
          ? this.chartData.length / this.pageSize
          : his.chartData.length / this.pageSize + 1;
      this.updateChart();
      this.startInterval();
    },

    updateChart() {
      const startPage = (this.currentPage - 1) * this.pageSize;
      const endPage = this.currentPage * this.pageSize;
      const showData = this.chartData.slice(startPage, endPage);
      const yAxisData = showData.map((item) => item.name);
      const xAxisData = showData.map((item) => item.value);
      const dataOption = {
        yAxis: {
          data: yAxisData,
        },
        series: [
          {
            data: xAxisData,
          },
        ],
      };
      this.chartInstance.setOption(dataOption);
    },

    startInterval() {
      if (this.timer) {
        clearInterval(this.timer);
      }
      this.timer = setInterval(() => {
        this.currentPage++;
        if (this.currentPage > this.totalPage) {
          this.currentPage = 1;
        }
        this.updateChart();
      }, 3000);
    },

    screenAdapter() {
      const titleFontSize = (this.$refs['seller_ref'].offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize,
            },
          },
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0],
            },
          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
  },
  computed: {
    ...mapState(['theme']),
  },
  watch: {
    theme() {
      console.log('主题切换了');
      this.chartInstance.dispose(); // 销毁当前的图表
      this.initChart(); // 重新以最新的主题名称初始化图表对象
      this.screenAdapter(); // 完成屏幕的适配
      this.updateChart(); // 更新图表的展示
    },
  },
};
</script>

<style lang="scss" scoped></style>
